package com.example.service.impl;

import com.example.entity.*;
import com.example.mappers.*;
import com.example.service.SchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SchemeServiceImpl implements SchemeService {
    @Autowired
    private ArmyMapper armyMapper;
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private SchemeMapper schemeMapper;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private EnvironmentMapper environmentMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Army> RequestArmy() {

        return armyMapper.GetAllArmy();
    }

    @Override
    public Army RequestArmyByID(Integer army_id) {

        Army army = armyMapper.GetArmyById(army_id);
        return army;
    }
    @Override
    public List<Location> RequestLocation(Location location)
    {
        return locationMapper.GetLocationByLocation(location);
    }
    @Override
    public Location RequestLocationByID(Integer location_id)
    {
        return locationMapper.GetAllLocationByID(location_id);
    }
    /*
    * schemeMapper.GetMaxSchemeId() + 1//表示当前id最大值+1；
    * */
    @Override
    public Integer CreateScheme()
    {

        return schemeMapper.GetMaxSchemeId() + 1;
    }
    @Override
    public boolean AddSchemeMsg(Scheme scheme)
    {
        if (schemeMapper.GetSchemeBySchemeID(scheme.getScheme_id()) == null)
        {
            schemeMapper.AddScheme(scheme);
            return true;
        }
        else
            return false;
    }
    public String AddTask(Task task)
    {
        if (task.getTask_type() == 1 && schemeMapper.GetSchemeTask(task.getType_id(),task.getTask_number()) == null)
        {
            schemeMapper.AddSchemeTask(task);
            return "{'type_code':'scheme_task'}";
        }
        else if (task.getTask_type() == 2 && planMapper.GetPlanTask(task.getType_id(),task.getTask_number()) == null)
        {
            planMapper.AddPlanTask(task);
            return "{'type_code':'plan_task'}";
        }
        else if(task.getTask_type() == 3 && groupMapper.GetGroupTask(task.getType_id(),task.getTask_number()) == null)
        {
            groupMapper.AddGroupTask(task);
            return "{'type_code':'group_task'}";
        }
        else if(task.getTask_type() == 4 && teamMapper.GetTeamTask(task.getType_id(),task.getTask_number()) == null)
        {
            teamMapper.AddTeamTask(task);
            return "{'type_code':'team_task'}";
        }

        return "{'type_code':'error'}";
    }
    public List<Environment> RequestEnvironment(Integer location_id,Integer month_time)
    {
        List<Environment> environments = environmentMapper.GetEnvironment(location_id,month_time);
        return environments;
    }
    public List<Category> RequestEquipment(Integer army_id)
    {
        List<Equipment> equipments = equipmentMapper.GetEquipmentByArmyId(army_id);
        List<Category> categories = new ArrayList<>();
        HashMap<Integer,Category> map = new HashMap();
        for (int i=0;i<equipments.size();i++)
        {
            Category category = categoryMapper.GetCategoryById(equipments.get(i).getCategory_id());
            map.put(equipments.get(i).getCategory_id(),category);
        }
        Iterator iter =  map.entrySet().iterator();
        while (iter.hasNext())
        {
            Map.Entry entry = (Map.Entry) iter.next();
            Integer key = (Integer) entry.getKey();
            Category val = (Category)entry.getValue();
            List<Equipment> equipmentList = equipmentMapper.GetEquipmentByCategoryId(key);
            val.setEquipment_list(equipmentList);
            categories.add(val);
        }
        return categories;
    }
    public boolean AddSchemeEquipment(Integer scheme_id,List<Integer> equipment_id_list)
    {
        for(int i=0;i<equipment_id_list.size();i++)
        {
            schemeMapper.AddSchemeEquipment(scheme_id,equipment_id_list.get(i));
        }
        return true;
    }
    public String AddPlan(Plan plan)
    {
        Plan p = planMapper.GetPlan(plan.getScheme_id(),plan.getPlan_name());
        if (p == null) {
            planMapper.AddPlan(plan);
            p = planMapper.GetPlan(plan.getScheme_id(),plan.getPlan_name());
            return "{'type_code':'plan','plan_id':"+p.getPlan_id()+"}";
        }
        else
            return "{'type_code':'error'}";

    }
    public String AddGroup(Group group)
    {
        Group g = groupMapper.GetGroup(group.getPlan_id(),group.getGroup_name());
        if (g == null)
        {
            groupMapper.AddGroup(group);
            g = groupMapper.GetGroup(group.getPlan_id(),group.getGroup_name());
            return "{'type_code':'group','group_id':"+g.getGroup_id()+"}";
        }
        else
            return "{'type_code':'error'}";
    }
    public String AddTeam(Team team)
    {
        Team t =teamMapper.GetTeam(team.getGroup_id(),team.getTeam_name());
        if (t == null)
        {
            teamMapper.AddTeam(team);
            t =teamMapper.GetTeam(team.getGroup_id(),team.getTeam_name());
            return "{'type_code':'team','team':"+t.getGroup_id()+"}";
        }
        else
            return "{'type_code':'error'}";
    }
    public List<Department> RequestDepartment()
    {
        return departmentMapper.GetAllDepartment();
    }
    public String AddTeamDepartment(Team_Department team_department)
    {

        Integer person_number =  teamMapper.GetDepartmentNumber(team_department.getTeam_id(),team_department.getDepartment_id());
        if (person_number == null)
        {
            teamMapper.AddDepartNumber(team_department);
            return "{'type_code':'team_department'}";
        }
        return "{'type_code':'error'}";
    }
    public String AddTeamCategory(Team_Category team_category)
    {

        Integer person_number =  teamMapper.GetCategoryNumber(team_category.getTeam_id(),team_category.getCategory_id());
        if (person_number == null)
        {
            teamMapper.AddCategoryNumber(team_category);
            return "{'type_code':'team_department'}";
        }
        return "{'type_code':'error'}";
    }

    public String ModifySchemeMsg(Scheme scheme)
    {
        if (schemeMapper.GetSchemeBySchemeID(scheme.getScheme_id()) == null)
            return "{'type_code':'error'}";
        else
            schemeMapper.ModifyScheme(scheme);
        return "{'type_code':'scheme'}";
    }

    public String ModifyTask(Task task)
    {
        if(task.getTask_type() == 1 && schemeMapper.GetSchemeTask(task.getType_id(),task.getTask_number()) != null)
        {
            schemeMapper.ModifySchemeTask(task);
            return "{'type_code':'scheme_task'}";
        }
        else if(task.getTask_type() == 2 && planMapper.GetPlanTask(task.getType_id(),task.getTask_number()) != null)
        {
            planMapper.ModifyPlanTask(task);
            return "{'type_code':'plan_task'}";
        }
        else if(task.getTask_type() == 3 && groupMapper.GetGroupTask(task.getType_id(),task.getTask_number()) != null)
        {
            groupMapper.ModifyGroupTask(task);
            return "{'type_code':'group_task'}";
        }
        else if(task.getTask_type() == 4 && teamMapper.GetTeamTask(task.getType_id(),task.getTask_number()) != null)
        {
            teamMapper.ModifyTeamTask(task);
            return "{'type_code':'team_task'}";
        }
        return "{'type_code':'error'}";
    }

    public List<Task> RequestTask(Integer type_id,Integer type)
    {
        List<Task> list = new ArrayList<>();
        if (type == 1)
        {
            list = schemeMapper.GetSchemeTask(type_id,null);
        }
        else if(type ==2)
        {
            list = planMapper.GetPlanTask(type_id,null);
        }
        else if(type ==3)
        {
            list = groupMapper.GetGroupTask(type_id,null);
        }
        else if(type ==4)
        {
            list = teamMapper.GetTeamTask(type_id,null);
        }
        return list;
    }

    @Override
    public List<Plan> RequestPlan(Integer scheme_id) {
        List<Plan> list = planMapper.GetPlanBySchemeId(scheme_id);
        for (int i=0; i<list.size(); i++)
        {
            List<Task> task_list = planMapper.GetPlanTask(scheme_id,null);
            list.get(i).setTask_list(task_list);
        }
        return list;
    }

    @Override
    public List<Group> RequestGroup(Integer plan_id) {
        List<Group> list = groupMapper.GetGroupByPlanId(plan_id);
        for (int i=0; i<list.size(); i++)
        {
            List<Task> task_list = groupMapper.GetGroupTask(plan_id,null);
            list.get(i).setTask_list(task_list);
        }
        return groupMapper.GetGroupByPlanId(plan_id);
    }

    @Override
    public List<Team> RequestTeam(Integer team_id) {
        List<Team> list =  teamMapper.GetTeamByGroupId(team_id);
        for (int i=0; i<list.size(); i++)
        {
            List<Task> task_list = teamMapper.GetTeamTask(team_id,null);
            list.get(i).setTask_list(task_list);
        }
        return list;
    }

    @Override
    public String DeleteTask(Integer task_id, Integer task_type) {
        return null;
    }

    @Override
    public String DeleteTeamDepartment(Team_Department team_department) {
        try
        {
            teamMapper.DeleteDepartment(team_department.getTeam_id(),team_department.getDepartment_id());
        }
        catch (Exception e)
        {

        }
        return null;
    }

    @Override
    public String DeleteTeamCategory(Team_Category team_category) {
        teamMapper.DeleteCategory(team_category.getTeam_id(),team_category.getCategory_id());
        return null;
    }

    @Override
    public String DeleteTeam(Integer team_id) {
        teamMapper.DeleteTeam(team_id);
        return null;
    }

    @Override
    public String DeleteTeamList(Integer group_id) {

        return null;
    }

    @Override
    public String DeleteGroup(Integer group_id) {
        return null;
    }

    @Override
    public String DeleteGroupList(Integer plan_id) {
        return null;
    }

    @Override
    public String DeletePlan(Integer plan_id) {
        return null;
    }

    @Override
    public String DeletePlanList(Integer scheme_id) {
        return null;
    }

    @Override
    public String DeleteScheme(Integer scheme_id) {
        return null;
    }
}
